import {
    getAuthList
} from './models/getAuthList.model';
import {
    editView
} from './views/edit.view';
import {
    validate
} from './validate';
import {
    submit
} from './submit';
import{
    msg,modal
}from '../common/layer';


const editBtnHandler = () => {

    let selectedList = $('#table').bootstrapTable('getSelections');
    if (selectedList.length === 0) {
        msg('请选择一项编辑');
        return;
    }

    const localData = selectedList[0];

    getAuthList(localData.id, function (data) {

      
        modal('编辑', editView(localData,data));


        $(".modal-form input").iCheck({
            checkboxClass: 'icheckbox_minimal-blue',
            radioClass: 'iradio_minimal-blue'
        });

        $('#submitBtn').click(() => {
            let data = {};
            $.each($('#form').serializeArray(), (key, val) => {
                data[val.name] = val.value;
            });

            let authlist = $("#authlist .checked input").map(function () {
                return $(this).val();
            }).get().join(",");
            data.authlist = authlist;

            if (!validate(data)) {
                return;
            }
            data.id = localData.id;
            submit(GLOBAL_roleEditUrl, data);
        });


    });





};

export {
    editBtnHandler
}